<template>
	 
	<view>   
		<!-- #ifdef H5 -->
		<view v-for="(item, index) in styleConfig" :key="index">
			<component
				:is="item.name"
				:index="index"
				:dataConfig="item"
				@changeBarg="changeBarg"
				@changeTab="changeTab"
				:tempArr="tempArr"
				:iSshowH="iSshowH"
				@detail="goDetail"
			></component>
		</view> 
		<!-- #endif -->
		<!-- #ifdef MP -->
		<block v-for="(item, index) in styleConfig" :key="index">
			<activeParty v-if="item.name == 'activeParty'" :dataConfig="item"></activeParty>
			<articleList v-if="item.name == 'articleList'" :dataConfig="item"></articleList>
			<bargain v-if="item.name == 'bargain'" :dataConfig="item" @changeBarg="changeBarg"></bargain>
			<blankPage v-if="item.name == 'blankPage'" :dataConfig="item"></blankPage>
			<combination v-if="item.name == 'combination'" :dataConfig="item"></combination>
			<coupon v-if="item.name == 'coupon'" :dataConfig="item"></coupon>
			<customerService v-if="item.name == 'customerService'" :dataConfig="item"></customerService>
			<goodList v-if="item.name == 'goodList'" :dataConfig="item" @detail="goDetail"></goodList>
			<guide v-if="item.name == 'guide'" :dataConfig="item"></guide>
			<headerSerch v-if="item.name == 'headerSerch'" :dataConfig="item"></headerSerch>
			<liveBroadcast v-if="item.name == 'liveBroadcast'" :dataConfig="item"></liveBroadcast>
			<menus v-if="item.name == 'menus'" :dataConfig="item"></menus>
			<news v-if="item.name == 'news'" :dataConfig="item"></news>
			<promotionList v-if="item.name == 'promotionList'" :dataConfig="item" @changeTab="changeTab" :tempArr="tempArr" :iSshowH="iSshowH" @detail="goDetail"></promotionList>
			<richText v-if="item.name == 'richText'" :dataConfig="item"></richText>
			<seckill v-if="item.name == 'seckill'" :dataConfig="item"></seckill>
			<swiperBg v-if="item.name == 'swiperBg'" :dataConfig="item"></swiperBg>
			<swipers v-if="item.name == 'swipers'" :dataConfig="item"></swipers>
			<tabNav v-if="item.name == 'tabNav'" :dataConfig="item"></tabNav>
			<titles v-if="item.name == 'titles'" :dataConfig="item"></titles>
		</block> 
		<!-- #endif -->
		<view class="loadingicon acea-row row-center-wrapper" v-if="tempArr.length && styleConfig[styleConfig.length - 1].name == 'promotionList'">
			<text class="loading iconfont icon-jiazai" :hidden="loading == false"></text>
			{{ loadTitle }}
		</view>
		<!-- #ifdef MP -->
		<!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse" :isGoIndex="false"></authorize> -->
		<!-- #endif -->
		<!-- <couponWindow :window="isCouponShow" @onColse="couponClose" :couponImage="couponObj.image" :couponList="couponObj.list"></couponWindow> -->
		 <Message :checkUserMsg="checkUserMsg" v-if='checkUserMsg'></Message>
	</view>
</template>

<script>
const app = getApp();
import couponWindow from '@/components/couponWindow/index'
import Message from './components/message'
// import { getCouponV2 } from '@/api/api.js'
import { getShare } from '@/api/public.js';
// #ifdef H5
import mConfig from './components/index.js';
import {silenceAuth } from '@/api/public.js';
// #endif
// #ifdef MP
import authorize from '@/components/Authorize';
import activeParty from './components/activeParty';
import headerSerch from './components/headerSerch';
import swipers from './components/swipers';
import coupon from './components/coupon';
import articleList from './components/articleList';
import bargain from './components/bargain';
import blankPage from './components/blankPage';
import combination from './components/combination';
import customerService from './components/customerService';
import goodList from './components/goodList';
import guide from './components/guide';
import liveBroadcast from './components/liveBroadcast';
import menus from './components/menus';
import news from './components/news';
import promotionList from './components/promotionList';
import richText from './components/richText';
import seckill from './components/seckill';
import swiperBg from './components/swiperBg';
import tabNav from './components/tabNav';
import titles from './components/titles';
 

import { getTemlIds } from '@/api/api.js';
import { SUBSCRIBE_MESSAGE, TIPS_KEY } from '@/config/cache';

// #endif
import { mapGetters } from 'vuex';
import { getDiy, getIndexData, checkUserShareGift } from '@/api/api.js';
import { getGroomList } from '@/api/store.js';
import { goShopDetail } from '@/libs/order.js';
import { toLogin } from '@/libs/login.js';
export default {
	computed: mapGetters(['isLogin', 'uid']),
	components: {
		couponWindow,
		Message,
		// #ifdef H5
		...mConfig, 
		// #endif
		// #ifdef MP
		authorize, 
		activeParty,
		headerSerch,
		swipers,
		coupon,
		articleList,
		bargain,
		blankPage,
		combination,
		customerService,
		goodList,
		guide,
		liveBroadcast,
		menus,
		news,
		promotionList,
		richText,
		seckill,
		swiperBg,
		tabNav,
		titles, 
		// #endif
	},
	computed: mapGetters(['isLogin']),
	data() {
		return {
			styleConfig: [],
			tempArr: [],
			goodType: 3,
			loading: false,
			loadend: false,
			loadTitle: '下拉加载更多', //提示语
			page: 1,
			limit: this.$config.LIMIT,
			iSshowH: false,
			numConfig: 0,
			code:'',
			isCouponShow:false,
			couponObj:{},
			shareInfo:{},
			// 未领取礼品的弹框
			checkUserMsg: null, 
		};
	},
	onLoad(options) {
		const {
			state,
			scope
		} = options;
		uni.getLocation({
			type: 'wgs84',
			success: function(res) {
				try {
					uni.setStorageSync('user_latitude', res.latitude);
					uni.setStorageSync('user_longitude', res.longitude);
				} catch {}
			}
		});
		this.diyData();
		this.getIndexData();
		// #ifdef H5
		this.setOpenShare();
		// #endif
		// #ifdef MP
		this.getTemlIds();
		// #endif
		getShare().then(res => {
			this.shareInfo = res.data;
		})
	},
	watch:{
		isLogin:{
			deep:true, //深度监听设置为 true
				 handler:function(newV,oldV){
					// 优惠券弹窗
					var newDates = new Date().toLocaleDateString();
					if (newV) {
						try{
							var oldDate = uni.getStorageSync('oldDate') || ''
						}catch{}
						if(oldDate!=newDates){
							// this.getCoupon();
							
						}	
					}
			}
		}
	},
	onShow() {
		// 优惠券弹窗
		var newDates = new Date().toLocaleDateString();
		if (this.isLogin) {
			try{
				var oldDate = uni.getStorageSync('oldDate') || '' 
			}catch{}
			
			this.checkUserShareGift()
			if(oldDate!=newDates){
				// this.getCoupon();
			}	
		}
	},
	mounted() { 
		// 清空checkUserMsg 关闭弹框
		uni.$on('changeCheckUserMsg',() => {
			this.checkUserMsg = null
		})
	},
	methods: {
		// 优惠券弹窗
		// getCoupon(){
		// 	getCouponV2().then(res=>{
		// 		this.couponObj = res.data
		// 		if(res.data.list.length>0){
		// 			this.isCouponShow = true
		// 		}
		// 	})
		// },
		// 优惠券弹窗关闭
		couponClose(){
			this.isCouponShow = false
			try {
				uni.setStorageSync('oldDate', new Date().toLocaleDateString());
			} catch {}
		},	
		onLoadFun() {},
		// #ifdef H5
		// 获取url后面的参数
		getQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i");
			var r = window.location.search.substr(1).match(reg);
			var q = window.location.pathname.substr(1).match(reg_rewrite);
			if(r != null){
					return unescape(r[2]);
			}else if(q != null){
					return unescape(q[2]);
			}else{
					return null;
			}
		},
		// #endif
		
		// #ifdef MP
		getTemlIds() {
			let messageTmplIds = wx.getStorageSync(SUBSCRIBE_MESSAGE); 
			if (!messageTmplIds) {
				getTemlIds().then(res => {
					if (res.data) wx.setStorageSync(SUBSCRIBE_MESSAGE, JSON.stringify(res.data));
				});
			}
		},
		// #endif
		// 对象转数组
		objToArr(data) { 
			let obj = Object.keys(data); 
			let m = obj.map(key => data[key]);
			return m;
		},
		diyData() {
			let that = this;
			getDiy().then(res => {
				that.styleConfig = that.objToArr(res.data);    
				that.objToArr(res.data).forEach(item => {
					if (item.name == 'promotionList') {
						that.numConfig = item.numConfig.val; 
						that.getGroomList();
					}
				});
			});
		},
	  
		checkUserShareGift(){
			checkUserShareGift().then(res => {
				if(res.data.status == 200){ 
					this.checkUserMsg = res.msg
				} 
			})
		},
		getIndexData() {
			// getIndexData().then(res => {
			// 	console.log(res.data);
			// })
		},
		changeBarg(item) {
			if (!this.isLogin) {
				toLogin();
			} else {
				uni.navigateTo({
					url: `/pages/activity/goods_bargain_details/index?id=${item.id}&bargain=${this.uid}`
				});
			}
		},
		// 促销列表的点击事件；
		changeTab(type) {
			this.goodType = type;
			this.tempArr = [];
			this.page = 1;
			this.loadend = false;
			let onloadH = true;
			this.getGroomList(onloadH);
		},
		// 精品推荐
		getGroomList(onloadH) {
			let that = this;
			let type = that.goodType;
			if (that.loadend) return false;
			if (that.loading) return false;
			if (onloadH) {
				that.$set(that, 'iSshowH', true);
			}
			getGroomList(type, {
				page: that.page,
				limit: that.limit
			})
				.then(({ data }) => {
					that.$set(that, 'iSshowH', false);
					let maxPage = Math.ceil(this.numConfig / this.limit);
					let list = data.list, 
						loadend = list.length < that.limit || that.page >= maxPage;
					let tempArr = that.$util.SplitArray(list, that.tempArr);
					that.$set(that, 'tempArr', tempArr.slice(0, this.numConfig));
					that.loadend = loadend;
					that.loadTitle = loadend ? '我也是有底线的' : '下拉加载更多';
					that.page = that.page + 1;
					that.loading = false;
				})
				.catch(res => {
					that.loading = false;
					that.loadTitle = '下拉加载更多';
				});
		},
		goDetail(item) {
				goShopDetail(item, this.uid).then(res => {
					uni.navigateTo({
						url: `/pages/goods_details/index?id=${item.id}`
					});
				});
			
		},
		// #ifdef H5
		// 微信分享；
		setOpenShare: function() {
			let that = this;
			if (that.$wechat.isWeixin()) {
				getShare().then(res => {
					let data = res.data.data;
					let configAppMessage = {
						desc: data.synopsis,
						title: data.title,
						link: location.href,
						imgUrl: data.img
					};
					that.$wechat.wechatEvevt(['updateAppMessageShareData', 'updateTimelineShareData'], configAppMessage);
				});
			}
		}
		// #endif
	},
	onReachBottom: function() {
		// this.getGroomList();
	},
	//#ifdef MP
	onShareAppMessage () {
	   return {
	    title: this.shareInfo.title,
	    path: '/pages/index/index',
	    imageUrl: this.storeInfo.img,
	   };
	},
	//#endif
};
</script>

<style></style>
